import { Component, OnInit, Injector } from '@angular/core';
import { NzTableHelper } from '@shared/helpers/NzTableHelper';
import { ServiceProxy } from '@shared/service-proxy/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'app-operation-logs',
  templateUrl: './operation-logs.component.html',
  styleUrls: ['./operation-logs.component.less']
})
export class OperationLogsComponent extends AppComponentBase implements OnInit {

  tableHelper: NzTableHelper;
  isVisible: boolean = false;
  keyword: string;
  roleId: string;
  roles = [];
  startTime: null;
  endTime: null;
  dateRange = [];

  constructor(
    injector: Injector,
    private _service: ServiceProxy) {
    super(injector);
    this.tableHelper = new NzTableHelper();
  }

  getRoles() {
    this._service.getAllRoleInfosUsingGET().subscribe(result => {
      this.roles = result.result.fieldList;
    });
  }


  ngOnInit() {
    this.load();
    this.getRoles();
    this.roleId = "";
  }
  load(pageIndex?: number) {
    if (typeof pageIndex !== 'undefined') {
      this.tableHelper.pageIndex = pageIndex || 1;
    }
    this.getList();
  }

  sort(sort: { key: string, value: string }): void {
    this.tableHelper.setSorting = sort;
    this.load();
  }

  getList() {
    this._service.getLogsBypageUsingGET(this.tableHelper.pageIndex, this.tableHelper.pageSize, this.keyword, this.roleId, undefined, this.startTime, this.endTime
    ).pipe(finalize(() => {
      this.tableHelper.loading = false;
    })).subscribe((result) => {
      this.tableHelper.items = result.result.fieldList;
      this.tableHelper.totalCount = result.result.totalRow;
    });
  }


  reset() {
    this.keyword = undefined;
    this.roleId = '';
    this.startTime = null;
    this.endTime = null;
    this.dateRange = [];
    this.load();
  }

  onChange(result: any): void {
    if (result) {
      this.startTime = result[0];
      this.endTime = result[1];
    }
    console.log('onChange: ', result);
  }
}
